package com.sun.dao;

import com.sun.dto.TypeAndAmount;
import com.sun.pojo.Type;
import org.apache.ibatis.annotations.*;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author: glorysun
 * @date: 2020/8/5
 */
@Mapper
@Repository
public interface TypeDao {

    @Insert("insert into blog.t_type values (#{id},#{name})")
    int insertType(Type type);   //新增分类

    @Select("select * from blog.t_type where id=#{id}")
    Type selectTypeById(Long id);  //根据id查询分类

    @Select("select * from blog.t_type where name=#{name}")
    Type selectTypeByName(String name);  //根据分类名查询分类

    @Select("select * from blog.t_type")
    List<Type> selectTypeList();  //分页查询分类

    @Update(" update blog.t_type set name = #{name} where id = #{id};")
    int updateType(Type type);     //更新分类

    @Delete("delete from blog.t_type where id = #{id}")
    int deleteType(Long id);    //删除分类

    @Select("<script>select b.type_id,count(b.type_id) as amount,t.name\n" +
            "from t_blog b,t_type t\n" +
            "where b.type_id=t.id\n" +
            "GROUP BY b.type_id\n" +
            "ORDER BY amount desc </script>")
    List<TypeAndAmount> selectTypeAndAmount();      //查询分类以及博客数量
}
